package xyz.keyon.servlet;

import xyz.keyon.bean.Student;
import xyz.keyon.service.IStudentService;
import xyz.keyon.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UpdateStudentBySnoServlet", urlPatterns = "/UpdateStudentBySnoServlet")
public class UpdateStudentBySnoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取响应头信息
        String sno = request.getParameter("sno");
        String sname = request.getParameter("sname");
        int sage = Integer.parseInt(request.getParameter("sage"));
        String saddress = request.getParameter("saddress");
        Student student = new Student(sno,sname,sage,saddress);
        //调用业务逻辑层
        IStudentService service = new StudentServiceImpl();
        boolean result = service.updateStudentBySno(student);
        System.out.println(result);
        //处理返回结果
        if(result) {
            //增加成功
            request.setAttribute("result", "success");
            System.out.println("修改成功");
            //页面跳转（请求转发）
            response.sendRedirect("QueryAllStudentsByPageServlet");//修改完毕后，再次重新查询全部的学生并显示
        } else {
            //增加失败
            request.setAttribute("result", "failed");
            System.out.println("修改失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
